package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.uitl.JsonResult;
import cn.wolfcode.wolf2w.uitl.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ITravelCommentService travelCommentService;
    //游记高级分页
    @GetMapping("/query")
    public Object queryTravels(TravelQuery qo){
        return JsonResult.success(travelService.query(qo));
    }
    //单个游记查询
    @GetMapping("/detail")
    public Object detail(String id){
        return JsonResult.success(travelService.get(id));
    }
    //百度那图片的富文档插件
    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);
        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    //单查询目的的,还有回显目的地
    @GetMapping("/input")
    public Object input(String id){
        //创建一个集合
       Map<String, Object> map = new HashMap<>();
       //查询所有目的地
        List<Destination> destinations = destinationService.lists();
        //添加时候回显用到
        if (StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            map.put("tv",travel);
        }
        map.put("dests",destinations);
        return JsonResult.success(map);
    }
    //要返回一个游记id
    @RequireLogin //要登录才可以操作
    @PostMapping ("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        //拿到当前用户对象
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoService.getKeyUser(token);
        travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        //MongoDB可以直接返回id的
        return JsonResult.success(travel.getId());
    }
    @RequireLogin //要登录才可以操作
    @PostMapping ("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo){
        travelCommentService.commentAdd(travelComment,userInfo);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(String travelId){
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }

}
